package me.alide.modules.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.alide.modules.survey.dao.OrgInfoEntityDao;
import me.alide.modules.survey.entity.OrgInfoEntity;
import me.alide.modules.survey.service.OrgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Anzhen
 * @description:
 * @PROJECT_NAME: alide
 * @create: 2021-07-25 14:32
 **/
@Service
public class OrgInfoServiceImpl implements OrgInfoService {
    @Autowired
    private OrgInfoEntityDao orgInfoEntityDao;

    @Override
    public int save(OrgInfoEntity orgInfoEntity) {
        return orgInfoEntityDao.insert(orgInfoEntity);
    }

    @Override
    public int delete(Long[] ids) {

        LambdaQueryWrapper<OrgInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(OrgInfoEntity::getId, ids);
        return orgInfoEntityDao.delete(wrapper);

    }

    @Override
    public int update(OrgInfoEntity orgInfoEntity) {
        return orgInfoEntityDao.updateById(orgInfoEntity);
    }

    @Override
    public List<OrgInfoEntity> queryAll() {
        return orgInfoEntityDao.selectList(Wrappers.lambdaQuery());
    }

    @Override
    public OrgInfoEntity queryById(Long id) {
        return orgInfoEntityDao.selectById(id);
    }

    @Override
    public List<OrgInfoEntity> queryByUserId(Long userId) {
        LambdaQueryWrapper<OrgInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(OrgInfoEntity::getUserId, userId);
        return orgInfoEntityDao.selectList(wrapper);
    }

    @Override
    public IPage<OrgInfoEntity> search(String keyword, int page, int pageSize) {
        IPage<OrgInfoEntity> pageObj = new Page<>(page, pageSize);
        QueryWrapper<OrgInfoEntity> queryWrapper = new QueryWrapper<>();
        if(keyword != null && keyword.length() > 0) {
            queryWrapper.like("org_name", keyword);
        }
        return orgInfoEntityDao.selectPage(pageObj, queryWrapper);
    }
}
